package model

import business.*
import org.itheima.kotlin.game.core.Composer
import org.itheima.kotlin.game.core.Painter
import java.util.*

/**
 * created by lucus on 2019/4/26
 * e-mail: mcphilex1989@gmail.com
 * phone: 15652608401
 * 敌方坦克
 */
class Enemy(override var x: Int, override var y: Int) : Moveable, AutoFlyable, Blockable, AutoShotable, Sufferable,
    Destroyable {

    // 地方坦克血量
    override var blood: Int = 3

    override var mCurrentDirection: Direction = Direction.LEFT
    override val speed: Int = 8
    override val width: Int = BaseConfig.GAME_UNIT
    override val height: Int = BaseConfig.GAME_UNIT
    private var badDirection: Direction? = null
    private var lastShotTime = 0L
    private val shotFrequency = 800
    private var lastMoveTime = 0L
    private val moveFrequency = 20

    override fun draw() {
        val tankPath = when (mCurrentDirection) {
            Direction.UP -> "img/enemy_4_u.gif"
            Direction.DOWN -> "img/enemy_4_d.gif"
            Direction.LEFT -> "img/enemy_4_l.gif"
            Direction.RIGHT -> "img/enemy_4_r.gif"
        }
        Painter.drawImage(tankPath, x, y)
    }

    override fun notifyCollision(direction: Direction?, blockable: Blockable?) {
        this.badDirection = direction
    }

    override fun autoFly() {
        val currentTimeMillis = System.currentTimeMillis()
        if (currentTimeMillis - lastMoveTime < moveFrequency) {
            return
        } else {
            lastMoveTime = currentTimeMillis
        }


        if (mCurrentDirection == this.badDirection) {
            // 随机生成新的方向
            mCurrentDirection = randomAvailableDirection(this.badDirection)
            return
        }

        when (mCurrentDirection) {
            Direction.UP -> y -= speed
            Direction.DOWN -> y += speed
            Direction.LEFT -> x -= speed
            Direction.RIGHT -> x += speed
        }

        if (x < 0) x = 0
        if (x > BaseConfig.GAME_WINDOW_WIDTH - width) x = BaseConfig.GAME_WINDOW_WIDTH - width
        if (y < 0) y = 0
        if (y > BaseConfig.GAME_WINDOW_HEIGHT - height) y = BaseConfig.GAME_WINDOW_HEIGHT - height
    }

    /**
     * 随机生成可用的方向
     */
    private fun randomAvailableDirection(badDirection: Direction?): Direction {
        val direction = when (Random().nextInt(4)) {
            0 -> Direction.UP
            1 -> Direction.DOWN
            2 -> Direction.LEFT
            3 -> Direction.RIGHT
            else -> Direction.DOWN
        }
        if (direction == badDirection) {
            return randomAvailableDirection(badDirection)
        }
        return direction

    }

    override fun autoShot(): IView? {
        val currentTimeMillis = System.currentTimeMillis()
        if (currentTimeMillis - lastShotTime < shotFrequency) {
            return null
        } else {
            lastShotTime = currentTimeMillis
        }

        return Bullet(this, mCurrentDirection) { bulletWidth, bulletHeight ->

            val tankX = x
            val tankY = y
            val tankWidth = width
            val tankHeight = height

            var bulletX: Int = 0
            var bulletY: Int = 0

            // 计算 子弹的坐标
            when (mCurrentDirection) {
                Direction.UP -> {
                    bulletX = tankX + (tankWidth - bulletWidth) / 2
                    bulletY = tankY - bulletHeight / 2
                }

                Direction.DOWN -> {
                    bulletX = tankX + (tankWidth - bulletWidth) / 2
                    bulletY = tankY + tankHeight - bulletHeight / 2
                }

                Direction.RIGHT -> {
                    bulletX = tankX + tankWidth - bulletWidth / 2
                    bulletY = tankY + (tankHeight - bulletHeight) / 2
                }

                Direction.LEFT -> {
                    bulletX = tankX - bulletWidth / 2
                    bulletY = tankY + (tankHeight - bulletHeight) / 2
                }

            }
            Pair(bulletX, bulletY)
        }
    }


    override fun notifySuffer(attackable: Attackable): Array<IView>? {
        if (attackable.owner is Enemy) return null
        blood -= attackable.attackBlood
        return arrayOf(Blast(x, y))
    }

    override fun isDestroy(): Boolean = blood <= 0


}